#coding:utf-8
import os
import sys
pwd = os.getcwd()
sys.path.append(pwd)
import argparse
from config.config import Config
from train_data import genera_data

conf=Config()

parser = argparse.ArgumentParser()

parser.add_argument('--home_dir_', dest='home_dir_', type=str, default='/home/yzfu/nlp/kg_abc/abc_project_data/',
                    help='path to the root dir')
parser.add_argument('--text_Path_', dest='text_Path_', type=str, default='text_01/',
                    help='path to the origin text')
parser.add_argument('--product_03_', dest='product_03_', type=str, default='product_03/',
                    help='path to the origin text')
parser.add_argument('--mark_path_', dest='mark_path_', type=str, default='mark.txt',
                        help='path to the mark tags')

args = parser.parse_args()

params = vars(args)
home_dir = params['home_dir_']
text_path_ = params['text_Path_']
product_03_ = params['product_03_']
mark_path_ = params['mark_path_']

mark_path = home_dir+mark_path_
print "mark_path:{}".format(mark_path)
#第二部執行，生成tag文件

mark_dict = genera_data.fyz_get_mark_info(mark_path)
# 生产数据
def productTag(orgin_path):
    file_name = orgin_path.split("/")[-1]
    path = home_dir+ 'train_result_tag.txt'

    if not os.path.exists(home_dir + product_03_):
        os.mkdir(home_dir + product_03_)
    if not os.path.exists(home_dir + 'test_03/'):
        os.mkdir(home_dir + 'test_03/')

    trainpath = home_dir + product_03_+file_name+'_product.data'
    testpath = home_dir + 'test_03/'+file_name+'_tag_test.data'
    #print  '==============================='+file_name+'==============================================='
    genera_data.hash_fyz_pos_word(orgin_path,path,mark_dict)
    #lstm_convertTag(path, trainpath, testpath,tag='2')
    print '文件：'+file_name


# 遍历指定目录，显示目录下的所有文件名
def eachFile(filepath):
    pathDir = os.listdir(filepath)
    file_names = []
    for allDir in pathDir:
        child = os.path.join('%s%s' % (filepath, allDir))
        child.decode('gbk')  # .decode('gbk')是解决中文显示乱码问题
        file_names.append(child)
    print '原始文档目录扫描完成'
    return file_names

# 词性标注
def segment_doc():
    dir_path = home_dir + text_path_
    file_names = eachFile(dir_path)
    for file_name_path in file_names:
        productTag(file_name_path)
    print '文件标注完成！！'

#文件格式批量转换
def change_file_format(file_paths):
    # 文件路径准备
    route =file_paths
    # 遍历路径下目录，文件夹，文件
    for root, dirs, files in os.walk(route):
        # 遍历文件
        for name in files:
            print "文件名; "+name
            # 拼接文件名(目录+文件名称)
            catalog = os.path.join(root, name)
            # 把所有行分割符替换为换行符\n返回.
            fp = open(catalog, "rU+")
            # 读取文件并保存
            strings = fp.read()
            fp.close()
            # 使用二进制写文件
            fp1 = open(catalog, "wb")
            fp1.seek(0)
            fp1.write(strings)
            fp1.flush()
            fp1.close()
    print '文件转换完成！'

if __name__ == "__main__":
    segment_doc()
